﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Configuration;

namespace CurrentSprintStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
//                string activityTypes = ConfigurationManager.AppSettings["activityTypes"];
//                DateTime dtnow = DateTime.Now;
//                //Sprint currentSprint = new Sprint();
//                //currentSprint.FillAllCurrentSprintWorkItemsWithTasks(activityTypes);
//                string query =
//                    @"SELECT [System.Id],[System.AssignedTo], [System.WorkItemType], [System.State], [System.AssignedTo], [System.Title] FROM WorkItems WHERE 
//                                    [System.TeamProject] = 'advantage' and [System.Id] = '23939' ORDER BY [System.WorkItemType], [System.Id]";
//                //WorkItem rerult = Sprint.ReturnWorkItem(query);

                ////Microsoft.VSTS.Common.DescriptionHtml
                //string description = rerult[rerult.Fields["Microsoft.VSTS.Common.DescriptionHtml"].Name].ToString();
                //foreach (Field  VARIABLE in rerult.Fields )
                //{
                //    Console.WriteLine(VARIABLE.Name);
                //}
                //Console.WriteLine(rerult.Description);
                //DateTime dtnow1 = DateTime.Now;
                //TimeSpan diff = dtnow1 - dtnow;
                //Console.Write(diff.ToString());
                //Console.Read();
                //DiscussionActivity result = Sprint.ReturnDiscussionActivity("23939");

               // TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(GetCollectionUri(new string[]{}));

                //using (var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(GetCollectionUri(new string[] { })))
                //{
                //    // get a reference to the work item tracking service
                //    IGroupSecurityService gss = projectCollection.GetService<IGroupSecurityService>();
                //    Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);

                //    Identity[] UserId = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

                //    foreach (Identity user in UserId)
                //    {
                //        Console.WriteLine(user.AccountName);
                //        Console.WriteLine(user.DisplayName);
                //    }
                //    // workItemStore.Query(wiqlQuery);
                //}
               // tfs.EnsureAuthenticated();

                //IGroupSecurityService gss = tfs.GetService<IGroupSecurityService>();

                //Common.Helper.TestLoginToTfs();

                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
                Console.Read();
            }
            
        }

        public static Uri GetCollectionUri(string[] args)
        {
            String collectionUri;

            if ((args.Length > 0) && (!String.IsNullOrEmpty(args[0])) &&
                Uri.IsWellFormedUriString(args[0], UriKind.Absolute))
            {
                collectionUri = args[0];
            }
            else
            {
                collectionUri = Environment.GetEnvironmentVariable("TFS_COLLECTION_URI");
            }

            while (String.IsNullOrEmpty(collectionUri))
            {
                //Console.WriteLine(
                //    "Please enter your TFS Team Project Collection URI,\n" +
                //    "or you can set it in TFS_COLLECTION_URI environment variable:");

                //collectionUri = Console.ReadLine();
                collectionUri = StaticItems.TfsUrl;

                if (!Uri.IsWellFormedUriString(collectionUri, UriKind.Absolute))
                {
                    collectionUri = null;
                }
            }

            return new Uri(collectionUri);
        }
    }
}
